//    Copyright (C) Kherty.  All rights reserved.
namespace OpenLS.Spreadsheet.Extensibility
{
    public class AllowStringToDoubleConversionAttribute : ParameterConverterAttribute
    {
        public AllowStringToDoubleConversionAttribute()
        {

        }
        public AllowStringToDoubleConversionAttribute(ReferenceOption refOption)
            : base(refOption)
        {

        }

        public bool Strict { get; set; }

        internal override void ConvertImp(ConversionData data)
        {

            if (data.Value is string && data.TargetType == typeof (double))
            {
                double dbl;
                if (double.TryParse((string)data.Value, out dbl))
                {
                    data.Result = dbl; return;
                }
                else if (!Strict)
                {
                    data.Result = 0.0d; return;
                }
                data.Error = Errors.ValueError;
            }
        }
    }
    public class AllowStringToIntConversionAttribute : ParameterConverterAttribute
    {
        public AllowStringToIntConversionAttribute()
        {

        }
        public AllowStringToIntConversionAttribute(ReferenceOption refOption)
            : base(refOption)
        {

        }

        public bool Strict { get; set; }

        internal override void ConvertImp(ConversionData data)
        {

            if (data.Value is string && data.TargetType == typeof(int))
            {
                int dbl;
                if (int.TryParse((string)data.Value, out dbl))
                {
                    data.Result = dbl; return;
                }
                else if (!Strict)
                {
                    data.Result = 0; return;
                }
                data.Error = Errors.ValueError;
            }
        }
    }
}